package ls.ch02.demo;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

/**
 * @author ls
 * @Date 创建时间:2022/4/6 17:05
 * 描述:
 */

public class Demo7 {
    public static void main(String[] args) {
        Document doc = null;
        try {
            //加载 DOM 树
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("F:\\idea\\S3-All\\frame\\src\\ls\\ch02\\demo\\Demo1.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取 XML 根节点
        Element root = doc.getRootElement();
        //遍历所有的 Brand 标签
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();) {
            Element brand = (Element) itBrand.next();
            //输出标签的 name 属性
            System.out.println("品牌:" + brand.attributeValue("name"));
            //遍历 Type 标签
            for (Iterator itType = brand.elementIterator(); itType.hasNext();) {
                org.dom4j.Element type = (Element) itType.next();
                //输出标签的 name 属性
                System.out.println("\t型号:" + type.attributeValue("name"));
            }
        }
    }
}
